package com.sgl.gk.controller;

import cn.hutool.http.HttpUtil;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.utils.ServletUtils;
import com.sgl.gk.controller.vo.DataTraceabilityInfosVo;
import com.sgl.gk.controller.vo.DataTraceabilityVo;
import com.sgl.gk.domain.GkStationConfigurationConnect;
import com.sgl.gk.scheduled.ScheduledTask;
import com.sgl.gk.service.GkStationConfigurationConnectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:xec
 * @date:2022/11/10 16:53
 *
 * 数据追溯页面controller
 **/

@RestController
@RequestMapping("/gk/datatranceability")
public class DataTranceabilityController extends BaseController {

    @Autowired
    GkStationConfigurationConnectService gkStationConfigurationConnectService;

    @Value("${spring.application.name}")
    public String serverKey ;

    @Value("${communication.mode}")
    public String communication;

    /**
     * 按条件分页查询数据追溯信息
     * @param gkStationConfigurationConnect  查询条件
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(GkStationConfigurationConnect gkStationConfigurationConnect){
        List<DataTraceabilityVo> list = gkStationConfigurationConnectService.list(gkStationConfigurationConnect);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 根据追溯id查询信息详情
     * @param id
     * @return
     */
    @GetMapping("/selectInfosById")
    public AjaxResult selectInfosById(Long id){
        return gkStationConfigurationConnectService.selectInfosById(id);
    }

    /**
     * 根据id列表批量导出追溯详情，压缩包形式导出
     * @param ids
     * @param gkStationConfigurationConnect
     * @param response
     */
    @RequestMapping(value = {"/exportProductInfos","/exportProductInfos/{ids}"})
    public void  exportProductInfos(@PathVariable(value = "ids",required = false) Long[] ids, @RequestBody(required = false) GkStationConfigurationConnect gkStationConfigurationConnect, HttpServletResponse response){
        gkStationConfigurationConnectService.exportProductInfos(ids,gkStationConfigurationConnect,response);
    }

    /**
     * 根据工位配置连接id和工位id导出
     * @param dataTraceabilityVo
     */
    @PostMapping("/exportByStationId")
    public void exportByStationId(@RequestBody DataTraceabilityVo dataTraceabilityVo,HttpServletResponse response){
        gkStationConfigurationConnectService.exportByStationId(dataTraceabilityVo,response);
    }

    /**
     * 根据单个id导出追溯信息
     * @param id
     * @param response
     */
    @GetMapping("/exportDataById")
    public void exportDataById(Long id,HttpServletResponse response){
        gkStationConfigurationConnectService.exportDataById(id,response);
    }

    /**
     * 查询过期时间
     * @return
     */
    @GetMapping("/expirationTime")
    public AjaxResult expirationTime(){
        String s = new String();
        if (communication.equals("plc")) {
            s = HttpUtil.get("127.0.0.1:12345/auth/getServerExpirationTime?serverKey=" + serverKey);
        }else if (communication.equals("socket")){
            s = "永久有效";
        }
        Map<String,String> map = new HashMap<>();
        map.put("expirationTime",s);
        return AjaxResult.success(map);
    }

    /**
     * 扫码匹配是否成功
     * @return
     */
    @GetMapping("/isMatching")
    public AjaxResult isMatching(){
        Map<String,Object> map = new HashMap<>();
        map.put("isMatching",ScheduledTask.isMatching);
        map.put("stationName",ScheduledTask.barCodeStationName);
        map.put("barCode",ScheduledTask.scanningBarCode);
        return AjaxResult.success(map);
    }

    /**
     * 扫码匹配失败，改变匹配状态
     * @return
     */
    @GetMapping("/editStatus")
    public AjaxResult editStatus(){
        ScheduledTask.isMatching = true;
        ScheduledTask.isMaching = false;
        ScheduledTask.readScanningCodeList.clear();
        ScheduledTask.isScanning = Boolean.FALSE;
        ScheduledTask.barCodeStationName = "";
        ScheduledTask.scanningBarCode = "";
        return AjaxResult.success();
    }

    @GetMapping("/test")
    public void test(Long connectId,Integer start,Integer end,String tableName){
        gkStationConfigurationConnectService.test(connectId,start,end,tableName);
    }
}
